﻿using EntityModel.CommodityMent;
using EntityModel.PageListInfor;
using IServices.ICurrency;
using IServices.IGoodsConfig;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace AikeApplets.Areas.GoodsConfigInfo.Controllers
{
    [Area("GoodsConfigInfo")]
    public class GoodsConfigInfoController : Controller
    {
        private readonly IGoodsCats _goodsCats;
        private readonly ICurrencyServer _currencyServer;

        public GoodsConfigInfoController(IGoodsCats goodsCats, ICurrencyServer currencyServer)
        {
            _goodsCats = goodsCats ?? throw new ArgumentNullException(nameof(goodsCats));
            _currencyServer = currencyServer ?? throw new ArgumentNullException(nameof(currencyServer));
        }

        #region 商品配置视图
        /// <summary>
        /// 商品类型视图
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加商品类型视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddGoodConfigDataType()
        {
            return View();
        }

        /// <summary>
        /// 修改商品类型信息
        /// </summary>
        /// <param name="catId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> UpdateGoodsCatsInfor(int catId)
        {
            return View(await _goodsCats.GetGoodsCatsDataByCatsidAsync(catId));
        }

        /// <summary>
        /// 商品分类视图
        /// </summary>
        /// <returns></returns>
        public IActionResult GoodsCategory()
        {
            return View();
        }

        #endregion

        #region 功能模块
        /// <summary>
        /// 显示商品类型信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="goods_Cats"></param>
        /// <returns></returns>
        public async Task<IActionResult> GetGoodConfigData(int page, int limit, AikeApplet_goods_cats goods_Cats)
        {
            if (page == 0) _currencyServer.Fail("页码不能为0");
            if (limit == 0) _currencyServer.Fail("每页显示条数不能为0");
            goods_Cats.PageIndex = page;
            goods_Cats.PageSize = limit;
            DbQueryResult data = await _goodsCats.GetGoodsCatsPageListAsync(goods_Cats);
            if (data.Data.Rows.Count == 0) return null;
            var result = data.Data.AsEnumerable().Select(s => new AikeApplet_goods_cats {
                catId = s.Field<int>("catId"),
                catName = s.Field<string>("catName"),
                seoDes = s.Field<string>("seoDes")
            });
            return Ok(new { code = 0, count = data.TotalRecords, data = result });
        }

        /// <summary>
        /// 获取商品父级信息
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetParentInfomation()
        {
            var data = await _goodsCats.GetParentInfomationBySql();
            if (data == null) return null;
            var result = data.AsEnumerable().Select(s => new AikeApplet_goods_cats {
                parentId = s.Field<int>("parentId"),
                catName = s.Field<string>("catName")
            });
            return Ok(new { result = result, code = "200", info = "数据查询成功" });
        }

        /// <summary>
        /// 添加商品类型信息
        /// </summary>
        /// <param name="aikeApplet_Goods"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddGoodCongInformation(AikeApplet_goods_cats aikeApplet_Goods)
        {
            var result = await _goodsCats.AddGoodsCatsInforAsync(aikeApplet_Goods);
            return _currencyServer.Success("数据添加成功");
        }

        /// <summary>
        /// 删除商品类型信息
        /// </summary>
        /// <param name="catId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RemoveGoodsCatsInfor(int catId)
        {
            var data = await _goodsCats.GetGoodsCatsDataByCatsidAsync(catId);
            if (data == null) return _currencyServer.Fail("数据已被删除");
            var result = await _goodsCats.DeletedGoodsCatsInforAsync(data);
            if (!result) return _currencyServer.Fail("数据删除失败");
            return _currencyServer.Success("数据删除成功");
        }

        [HttpPost]
        public async Task<IActionResult> UpdateGoodCongInformation(AikeApplet_goods_cats aikeApplet_Goods)
        {
            return View();
        }
        #endregion
    }
}
